<template>
  <div class="search-result-container">
    <!-- 点击实现后退效果 -->
    <van-nav-bar
      title="搜索结果"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 上拉加载更多 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多数据了"
      @load="onLoad"
      :immediate-check="false"
      
    >
      <!-- 文章的 Item 项 -->
      <artItem
        v-for="(item, index) in searchList"
        :key="index"
        :itemObj="item"
         :closable="false"
      ></artItem>
    </van-list>
  </div>
</template>

<script>
import { getSearchResultAPI } from "@/api/searchAPI.js";
import artItem from "@/components/artItem/artItem.vue";
export default {
  name: "SearchResult",
  data() {
    return {
      page: 1,
      searchList: [],
      // / 是否正在进行上拉加载的数据请求
      loading: false,
      // 所有数据是否已经加载完毕
      finished: false,
    };
  },
  methods: {
    async initSearchList() {
      const { data: res } = await getSearchResultAPI(
        this.$route.params.kw,
        this.page
      );
      if (res.message === "OK") {
        this.searchList = [...this.searchList, ...res.data.results];
        this.loading = false;
        // console.log(this.searchList);
        if (res.data.results.length == 0) {
          this.finished = true;
        }
        // 4. 让页码值 +1
        this.page += 1;
      }
    },
    onLoad() {
      this.initSearchList();
    },
  },
  created() {
    this.initSearchList();
  },
  components: {
    artItem,
  },
};
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>